package com.article.controller;

import com.article.common.PageParam;
import com.article.common.ResultBody;
import com.article.model.Article;
import com.article.model.Discuss;
import com.article.model.User;
import com.article.service.IArticleService;
import com.article.service.IDiscussService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author lx
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
	 private static final Logger log=LoggerFactory.getLogger(ArticleController.class);
	 @Resource
	 private IArticleService service;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "/article";
    }


    /**
     * 新增
     *
     * @return
     */
    @RequestMapping("/add")
    public String addData() {
        return "/articleAdd";
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping("/edit")
    public String editData(Model model, Integer id) {
        model.addAttribute("article",this.service.getOne(id));
        return "/articleEdit";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageParam list(PageParam pageParam) {
        return this.service.queryAll(pageParam);
    }

    /**
     * 新增
     * @param entity
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody add(HttpServletRequest request, Article entity){
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        entity.setCreateBy(user.getNickName());
        entity.setCreateTime(new Date());
        this.service.add(entity);
        return ResultBody.success("添加成功");
    }

    /**
     * 查找
     * @param id
     */
    @RequestMapping(value = "/getOne")
    @ResponseBody
    public ResultBody getOne(Integer id){
        Article obj=this.service.getOne(id);
        if(obj!=null){
            return ResultBody.success(obj);
        }else{
            return ResultBody.error("未获取到数据！");
        }
    }

    /**
     * 删除单条
     * @param id
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(Integer  id){
        this.service.delete(id);
        return ResultBody.success("删除成功");
    }

    /**
     * 删除多条
     * @param ids
     */
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody deleteByIds(Integer[]  ids){
        this.service.deleteByIds(ids);
        return ResultBody.success("删除成功");
    }

    /**
     * 修改
     * @param entity
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(HttpServletRequest request,Article entity){
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        entity.setUpdateBy(user.getNickName());
        this.service.update(entity);
        return ResultBody.success("修改成功");
    }


}
